//
//  MemberCentreViewController.m
//  cherryproduct
//
//  Created by Enter zhao on 14-8-12.
//  Copyright (c) 2014年 cherry. All rights reserved.
//

#import "MemberCentreViewController.h"
#import "ChangeMessageViewController.h"
#import "HttpNewBaseService.h"
#import "UIImageView+AFNetworking.h"
#import "GGUserInfoViewController.h"
#import "MemberTestViewController.h"
#import "FIGoodsDetailsViewController.h"
#import "BuyProductViewController.h"


@interface MemberCentreViewController()<UITableViewDataSource, UITableViewDelegate>
{
    NSDictionary* mainDic;
    
    NSMutableArray * mainArray;
    
    UIImageView * coverImageView;
    UIImageView * logoImageView;
    
    UITapGestureRecognizer * _tap;
    
    NSMutableArray * data_list;
    
    BOOL m_b_is_have_move;
    BOOL m_b_is_loading;
    NSInteger m_int_request_page;
}

@property(nonatomic, strong) UIView * view_bar_bg;
@property(nonatomic, strong) UITableView * tableview;

@end

@implementation MemberCentreViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)backButtonAction:(UIButton*) sender
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.myView.hidden = YES;
    [self setNavigationBarColor:[UIColor clearColor]];
    self.view.backgroundColor = [UIColor whiteColor];
    
    m_b_is_have_move=YES;
    m_b_is_loading=YES;
    data_list = [[NSMutableArray alloc] initWithCapacity:12];
    
    // Do any additional setup after loading the view.
    /*
    [self setTitle:@"个人中心"];
    self.myView.hidden = YES;
    [self addNavBarButton:RIGHT_SETTING];
    [self setNavigationBarColor:[UIColor clearColor]];
     */
    
    CGFloat width = self.view.frame.size.width;
    if (coverImageView==nil) {
        coverImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, width, 164)];
        coverImageView.contentMode=UIViewContentModeScaleAspectFill;
        coverImageView.clipsToBounds=YES;
        
        [coverImageView setImage:[UIImage imageNamed:@"user_bg"]];
        [self.view addSubview:coverImageView];
    }
    
    if (self.tableview == nil) {
        self.tableview = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        self.tableview.backgroundColor=[UIColor clearColor];
        self.tableview.dataSource=self;
        self.tableview.delegate=self;
        self.tableview.separatorStyle=UITableViewCellSeparatorStyleNone;
        self.tableview.showsVerticalScrollIndicator=NO;
        
        UIView * view_header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 164)];
        view_header.backgroundColor=[UIColor clearColor];
        {
            UIButton * btn_header = [[UIButton alloc] initWithFrame:CGRectMake(20, 70, 70, 70)];
            btn_header.backgroundColor=[UIColor clearColor];
            [btn_header addTarget:self action:@selector(doActionHeader:) forControlEvents:UIControlEventTouchUpInside];
            
            logoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
            logoImageView.userInteractionEnabled = YES;
            logoImageView.image = [UIImage imageNamed:@"bodyheader"];
            logoImageView.userInteractionEnabled=NO;
            
            logoImageView.layer.cornerRadius = 35;
            logoImageView.layer.masksToBounds = YES;
            logoImageView.layer.borderWidth = 3.0;
            logoImageView.layer.borderColor = [UIColor whiteColor].CGColor;
            
            [btn_header addSubview:logoImageView];
            
            [view_header addSubview:btn_header];
            
            //tap
            _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doActionBG:)];
            [view_header addGestureRecognizer:_tap];
        }
        self.tableview.tableHeaderView=view_header;
        
        [self.view addSubview:self.tableview];
    }

    if (self.view_bar_bg == nil) {
        self.view_bar_bg = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
        self.view_bar_bg.backgroundColor=[UIColor clearColor];
        [self.view addSubview:self.view_bar_bg];
        
        UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 20, 60, 44)];
        if (IOS7_OR_LATER) {
            [backButton setImage:[UIImage imageNamed:@"topBack.png"] forState:UIControlStateNormal];
            [backButton setImage:[UIImage imageNamed:@"topBack.png"] forState:UIControlStateHighlighted];
        } else {
            [backButton setBackgroundImage:[UIImage imageNamed:@"topBack.png"] forState:UIControlStateNormal];
            [backButton setBackgroundImage:[UIImage imageNamed:@"topBack.png"] forState:UIControlStateHighlighted];
        }
        [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view_bar_bg addSubview:backButton];
        
        UILabel * label_title = [[UILabel alloc] initWithFrame:CGRectMake(50, 20, width-100, 44)];
        label_title.textColor = [UIColor whiteColor];
        label_title.backgroundColor = [UIColor clearColor];
        label_title.font = [UIFont systemFontOfSize:20];
        label_title.textAlignment = NSTextAlignmentCenter;
        label_title.text = @"个人中心";
        [self.view_bar_bg addSubview:label_title];
        
        UIButton *phoneButton = [[UIButton alloc] initWithFrame:CGRectMake(DEVICE_BOUNDS_WIDTH-44, IOS7_OR_LATER?20:0, [UIImage imageNamed:@"setting.png"].size.width-0, 44)];
        [phoneButton setImage:[UIImage imageNamed:@"setting.png"] forState:UIControlStateNormal];
        [phoneButton setImage:[UIImage imageNamed:@"setting.png"] forState:UIControlStateHighlighted];
        [phoneButton addTarget:self action:@selector(settingButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view_bar_bg addSubview:phoneButton];
    }
    
    [self getFeedData];
    //[self getUserData];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    {
        NSString * new_logo = [[LoginViewController shareUserInfo] objectForKey:@"new_logo"];
        NSString * new_cover = [[LoginViewController shareUserInfo] objectForKey:@"new_cover"];
        if (new_logo) {
            [logoImageView setImageWithURL:[NSURL URLWithString:new_logo]
                          placeholderImage:[UIImage imageNamed:@"bodyheader"]];
        }
        if (new_cover) {
            [coverImageView setImageWithURL:[NSURL URLWithString:new_cover]
                           placeholderImage:[UIImage imageNamed:@"user_bg"]];
        }
    }
    
    if (self.m_b_is_reload) {
        self.m_b_is_reload=NO;
        
        if (self.tableview) {
            [self.tableview reloadData];
        }
    }
}

- (void)doActionHeader:(id)sender
{
    
}
- (void)doActionBG:(id)sender
{
    
}

#pragma mark -
#pragma mark MarkHeader

static CGFloat ImageHeight  = 164.f;
static CGFloat ImageWidth  = 320.0;

- (void)updateImg {
    CGFloat yOffset   = self.tableview.contentOffset.y;
    CGFloat f_height = 0;
    
    if (yOffset < 0) {
        CGFloat factor = ((ABS(yOffset)+ImageHeight)*ImageWidth)/ImageHeight;
        CGRect f = CGRectMake(-(factor-ImageWidth)/2, f_height, factor, ImageHeight+ABS(yOffset));
        coverImageView.frame = f;
    } else {
        CGRect f = coverImageView.frame;
        f.origin.y = f_height-yOffset;
        coverImageView.frame = f;
    }
    
    if (yOffset<100) {
        self.view_bar_bg.backgroundColor=[UIColor clearColor];
    } else {
        self.view_bar_bg.backgroundColor=RGBACOLOR(0, 0, 0, 0.5);
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self updateImg];
    
    //加载更多
    {
        CGRect frame = scrollView.frame;
        CGSize size = scrollView.contentSize;
        CGPoint point = scrollView.contentOffset;
        
        if (point.y > (size.height-frame.size.height)) {
            if (m_b_is_have_move && m_b_is_loading==NO) {
                m_b_is_loading = YES;
                [self loadMoreData];
            }
        }
    }
}

- (void)loadMoreData
{
    m_b_is_loading=YES;
    m_int_request_page++;
    
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                 [NSNumber numberWithInteger:m_int_request_page], @"p",
                                 nil];
    if (self.m_this_page_type>0) {
        [dic setObject:[NSNumber numberWithInteger:self.m_this_page_type] forKey:@"type"];
    }
    
    [[HttpNewBaseService sharedClient] postRequestOperation:@"member/feed" paramDictionary:dic onCompletion:^(NSString *responseString) {
        m_b_is_loading=NO;
        [self.view stopLoadingAnimation];
        @try {
            NSDictionary * dicjson = [responseString JSONValue];
            if ([[dicjson objectForKey:@"status"] intValue] == 1) {
                {
                    //getDataList
                    NSDictionary * data = [dicjson objectForKey:@"data"];
                    if (data) {
                        NSDictionary * feed = [data objectForKey:@"feed"];
                        if (feed && ![feed isKindOfClass:[NSNull class]]) {
                            for (id item in feed) {
                                [data_list addObject:item];
                            }
                            
                            //判断是否有更多
                            if ([feed count] >= 10) {
                                m_b_is_have_move=YES;
                            } else {
                                m_b_is_have_move=NO;
                            }
                        } else {
                            m_b_is_have_move=NO;
                        }
                    }
                    [self.tableview reloadData];
                    return;
                }
            }
            else
            {
                [self showAlertView:@"温馨提示" subtitle:[dicjson objectForKey:@"message"]];
            }
        }
        @catch (NSException *exception) {
            m_b_is_have_move=NO;
            [self showAlertView:@"温馨提示" subtitle:@"服务器异常！"];
        }
        @finally {
            ;
        }
    } onError:^(NSError *error) {
        m_b_is_loading=NO;
        [self.view stopLoadingAnimation];
        [self showAlertView:@"温馨提示" subtitle:@"网络断了！"];
    } onAnimated:YES];
}

#pragma mark -
#pragma mark So

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)settingButtonAction:(UIButton *)sender
{
    //ChangeMessageViewController* vc = [[ChangeMessageViewController alloc] init];
    GGUserInfoViewController * vc = [[GGUserInfoViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)getFeedData
{
    [data_list removeAllObjects];
    [self.tableview reloadData];
    self.tableview.contentSize=CGSizeMake(0, 0);
    
    [self.view startLoadingAnimation];
    
    m_b_is_loading=YES;
    m_int_request_page = 1;
    
    [[HttpNewBaseService sharedClient] postRequestOperation:@"member/feed" paramDictionary:nil onCompletion:^(NSString *responseString) {
        m_b_is_loading=NO;
        [self.view stopLoadingAnimation];
        @try {
            NSDictionary * dicjson = [responseString JSONValue];
            if ([[dicjson objectForKey:@"status"] intValue] == 1) {
                {
                    //getDataList
                    NSDictionary * data = [dicjson objectForKey:@"data"];
                    if (data) {
                        NSDictionary * feed = [data objectForKey:@"feed"];
                        if (feed && ![feed isKindOfClass:[NSNull class]]) {
                            for (id item in feed) {
                                [data_list addObject:item];
                            }
                            
                            //判断是否有更多
                            if ([feed count] >= 10) {
                                m_b_is_have_move=YES;
                            } else {
                                m_b_is_have_move=NO;
                            }
                        } else {
                            m_b_is_have_move=NO;
                        }
                    }
                    [self.tableview reloadData];
                    return;
                }
            }
            else
            {
                [self showAlertView:@"温馨提示" subtitle:[dicjson objectForKey:@"message"]];
            }
        }
        @catch (NSException *exception) {
            m_b_is_have_move=NO;
            [self showAlertView:@"温馨提示" subtitle:@"服务器异常！"];
        }
        @finally {
            ;
        }
    } onError:^(NSError *error) {
        m_b_is_loading=NO;
        [self.view stopLoadingAnimation];
        [self showAlertView:@"温馨提示" subtitle:@"网络断了！"];
    } onAnimated:YES];
}
- (void)getUserData
{
    [[HttpNewBaseService sharedClient] getRequestOperation:@"member/get_profile" paramDictionary:nil onCompletion:^(NSString *responseString) {
        @try {
            
            NSDictionary* dic =[responseString JSONValue];
            if ([[dic objectForKey:@"status"] intValue] == 1) {

                
                NSMutableString* stringimageurl = [[NSMutableString alloc] init];
                [stringimageurl appendString:JPG];
                [stringimageurl appendString:[[dic objectForKey:@"data"] objectForKey:@"cover"]];
                [coverImageView setImageWithURL:[NSURL URLWithString:stringimageurl] placeholderImage:nil];
                
                NSMutableString* stringimageurl1 = [[NSMutableString alloc] init];
                [stringimageurl1 appendString:JPG];
                [stringimageurl1 appendString:[[dic objectForKey:@"data"] objectForKey:@"logo"]];
                [logoImageView setImageWithURL:[NSURL URLWithString:stringimageurl1] placeholderImage:nil];
                
            }
            else
            {
                //[self showAlertView:@"温馨提示" subtitle:[dic objectForKey:@"message"]];
            }
        }
        @catch (NSException *exception) {
            //[self showAlertView:@"温馨提示" subtitle:@"网络通讯失败！"];
        }
        @finally {
            ;
        }
        
    } onError:^(NSError *error) {
        //[self showAlertView:@"温馨提示" subtitle:@"网络断了！"];
    } onAnimated:YES];
}

- (void)PageRefThisPage
{
    [data_list removeAllObjects];
    [self.tableview reloadData];
    self.tableview.contentSize=CGSizeMake(0, 0);
    
    //请求不同类型的数据
    [self.view startLoadingAnimation];
    
    m_b_is_loading=YES;
    m_int_request_page = 1;
    NSMutableDictionary * dic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                 [NSNumber numberWithInteger:m_int_request_page], @"p",
                                 [NSNumber numberWithInteger:self.m_this_page_type], @"type",
                                 nil];
    
    [[HttpNewBaseService sharedClient] postRequestOperation:@"member/feed" paramDictionary:dic onCompletion:^(NSString *responseString) {
        m_b_is_loading=NO;
        [self.view stopLoadingAnimation];
        @try {
            NSDictionary * dicjson = [responseString JSONValue];
            if ([[dicjson objectForKey:@"status"] intValue] == 1) {
                {
                    //getDataList
                    NSDictionary * data = [dicjson objectForKey:@"data"];
                    if (data) {
                        NSDictionary * feed = [data objectForKey:@"feed"];
                        if (feed && ![feed isKindOfClass:[NSNull class]]) {
                            for (id item in feed) {
                                [data_list addObject:item];
                            }
                            
                            //判断是否有更多
                            if ([feed count] >= 10) {
                                m_b_is_have_move=YES;
                            } else {
                                m_b_is_have_move=NO;
                            }
                        } else {
                            m_b_is_have_move=NO;
                        }
                    }
                    [self.tableview reloadData];
                    return;
                }
            }
            else
            {
                [self showAlertView:@"温馨提示" subtitle:[dicjson objectForKey:@"message"]];
            }
        }
        @catch (NSException *exception) {
            m_b_is_have_move=NO;
            [self showAlertView:@"温馨提示" subtitle:@"服务器异常！"];
        }
        @finally {
            ;
        }
    } onError:^(NSError *error) {
        m_b_is_loading=NO;
        [self.view stopLoadingAnimation];
        [self showAlertView:@"温馨提示" subtitle:@"网络断了！"];
    } onAnimated:YES];
}


#pragma mark -
#pragma mark UITableViewDataSource UITableViewDelegate

- (void)goRefPage:(NSInteger)page;
{
    if (page==1) {
        //团购提示
    }
    if (page==2) {
        //我参加的团购
    }
    if (page==3) {
        
    }
    if (page==4) {
        //我的评论
    }
    if (page==5) {
        //我点评的商品
    }
    if (self.m_this_page_type>0) {
        self.m_this_page_type=0;
        [self getFeedData];
    } else {
        //刷新当前页面
        self.m_this_page_type=page;
        [self PageRefThisPage];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (data_list) {
        return [data_list count]+1;
    }
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger index = indexPath.row;
    if (data_list) {
        if (index < [data_list count]) {
            return 160;
        }
    }
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * str_cell = @"cell_feed";
    
    SEFeedViewCell * cell = [tableView dequeueReusableCellWithIdentifier:str_cell];
    if (cell==nil) {
        cell = [[SEFeedViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str_cell];
    }
    [cell StateOP:0];
    cell.delegate_go_page=self;
    
    cell.ggLabel_SO_0.hidden=YES;
    cell.ggLabel_SO_1.hidden=YES;
    cell.ggLabel_SO_2.hidden=YES;
    
    cell.ggLabel_SO_0.text=@"";
    cell.ggLabel_SO_1.text=@"";
    cell.ggLabel_SO_2.text=@"";
    
    NSInteger index = indexPath.row;
    if (index < [data_list count]) {
        NSDictionary * dic = [data_list objectAtIndex:index];
        NSString * type = [dic objectForKey:@"type"];
        if (type) {
            NSInteger op_type = [type integerValue];
            [cell StateOP:op_type];
            if (op_type==2) {
                cell.ggLabel_SO_0.hidden=NO;
                
                NSString * str_bb = [[dic objectForKey:@"groupon_table"] objectForKey:@"end_time"];
                if (str_bb) {
                    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
                    [formatter setDateStyle:NSDateFormatterMediumStyle];
                    [formatter setTimeStyle:NSDateFormatterShortStyle];
                    [formatter setDateFormat:@"yyyy-MM-dd"];
                    
                    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:str_bb.integerValue];
                    NSString * str_end_time = [NSString stringWithFormat:@"活动结束时间:%@", [formatter stringFromDate:confromTimesp]];
                    
                    cell.ggLabel_SO_0.text=str_end_time;
                }
            }
            if (op_type==1) {
                cell.ggLabel_SO_1.hidden=NO;
                cell.ggLabel_SO_2.hidden=NO;
                
                NSString * str_bb = [[dic objectForKey:@"groupon_table"] objectForKey:@"start_time"];
                if (str_bb) {
                    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
                    [formatter setDateStyle:NSDateFormatterMediumStyle];
                    [formatter setTimeStyle:NSDateFormatterShortStyle];
                    [formatter setDateFormat:@"yyyy-MM-dd"];
                    
                    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:str_bb.integerValue];
                    NSString * str_end_time = [NSString stringWithFormat:@"活动开始时间:%@", [formatter stringFromDate:confromTimesp]];
                    
                    cell.ggLabel_SO_2.text=str_end_time;
                }
                
                NSString * str_alarm = [dic objectForKey:@"alarm"];
                if (str_alarm) {
                    cell.ggLabel_SO_1.text=str_alarm;
                }
            }
            
        }
        //设置时间
        {
            NSMutableString * str_time = [[NSMutableString alloc] initWithCapacity:30];
            if ([dic objectForKey:@"day"]) {
                [str_time appendString:[dic objectForKey:@"day"]];
            }
            if ([dic objectForKey:@"hour"]) {
                [str_time appendString:[dic objectForKey:@"hour"]];
            }
            cell.dataLabel.text=str_time;
        }
        //设置标题
        {
            NSString * str_title = [dic objectForKey:@"title"];
            if (str_title) {
                NSRange range=[str_title rangeOfString:@"\""];//指定的字符串从左往右匹配（系统默认）
                //指定的字符串从右往左匹配
                NSRange range1=[str_title rangeOfString:@"\"" options:NSBackwardsSearch];
                
                NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:str_title];
                [str addAttribute:NSForegroundColorAttributeName
                            value:RGBCOLOR(43, 124, 246)
                            range:NSMakeRange(range.location+1, range1.location-range.location-1)];
                cell.label_title.attributedText = str;
            } else {
                str_title = @"没有标题呢";
                cell.label_title.text=str_title;
            }
        }
        //设置图片
        {
            id groupon = [dic objectForKey:@"groupon"];
            if (groupon && [groupon isKindOfClass:[NSArray class]]) {
                int i = 0;
                for (id item in groupon) {
                    NSDictionary * goods = [item objectForKey:@"goods"];
                    if (goods && ![goods isKindOfClass:[NSNull class]]) {
                        NSString * photo = [goods objectForKey:@"photo"];
                        NSString * str_img_url = [NSString stringWithFormat:@"%@%@", JPG, photo];
                        
                        NSString * name = [goods objectForKey:@"name"];
                        
                        if (i==0) {
                            [cell.ggImage0 setImageWithURL:[NSURL URLWithString:str_img_url] placeholderImage:nil];
                            if (name) {
                                cell.ggLabel0.text=name;
                            }
                        }
                        if (i==1) {
                            [cell.ggImage1 setImageWithURL:[NSURL URLWithString:str_img_url] placeholderImage:nil];
                            if (name) {
                                cell.ggLabel1.text=name;
                            }
                        }
                        if (i==2) {
                            [cell.ggImage2 setImageWithURL:[NSURL URLWithString:str_img_url] placeholderImage:nil];
                            if (name) {
                                cell.ggLabel2.text=name;
                            }
                        }
                    }
                    
                    if (i==2) {
                        break;
                    }
                    i++;
                }
            } else {
                if (groupon && [groupon isKindOfClass:[NSDictionary class]]) {
                    NSDictionary * goods = [groupon objectForKey:@"goods"];
                    if (goods && ![goods isKindOfClass:[NSNull class]]) {
                        NSString * photo = [goods objectForKey:@"photo"];
                        NSString * str_img_url = [NSString stringWithFormat:@"%@%@", JPG, photo];
                        
                        NSString * name = [goods objectForKey:@"name"];
                        [cell.ggImage0 setImageWithURL:[NSURL URLWithString:str_img_url] placeholderImage:nil];
                        if (name) {
                            cell.ggLabel0.text=name;
                        }
                    }
                } else {
                    NSDictionary * goods = [dic objectForKey:@"goods"];
                    if (goods && ![goods isKindOfClass:[NSNull class]]) {
                        if (goods) {
                            NSString * photo = [goods objectForKey:@"photo"];
                            NSString * str_img_url = [NSString stringWithFormat:@"%@%@", JPG, photo];
                            NSString * name = [goods objectForKey:@"name"];
                            [cell.ggImage0 setImageWithURL:[NSURL URLWithString:str_img_url] placeholderImage:nil];
                            if (name) {
                                cell.ggLabel0.text=name;
                            }
                        }
                    }
                }
            }
        }
    } else {
        //加载显示
        if (m_b_is_have_move) {
            [cell StateLoading:1];
        } else {
            [cell StateLoading:0];
        }
    }
    
    return cell;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
//{
//    return 64;
//}
//
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
//{
//    CGFloat width = self.view.frame.size.width;
//    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, 64)];
//    view.backgroundColor=[UIColor redColor];
//    return view;
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger index = indexPath.row;
    if (index < [data_list count]) {
        NSDictionary * feed_data = [data_list objectAtIndex:index];
        if (feed_data && ![feed_data isKindOfClass:[NSNull class]]) {
            
            NSString * str_type = [feed_data objectForKey:@"type"];
            NSString * str_id = [feed_data objectForKey:@"fid"];
            
            if (str_type && str_id) {
                NSInteger ba_type = [str_type integerValue];
                if (ba_type==5) {//我的点评
                    self.m_b_is_reload=YES;
                    
                    NSDictionary * ddddd_dic = [feed_data objectForKey:@"goods"];
                    if (ddddd_dic && [ddddd_dic isKindOfClass:[NSDictionary class]]) {
                        NSString * dddd_id = [ddddd_dic objectForKey:@"id"];
                        if (dddd_id && [dddd_id isKindOfClass:[NSString class]]) {
                            MemberTestViewController * vc = [[MemberTestViewController alloc] init];
                            vc.str_id=dddd_id;
                            vc.out_data_list=data_list;
                            vc.select_index=indexPath.row;
                            [self.navigationController pushViewController:vc animated:YES];
                        }
                    }
                }
                if (ba_type==1) {//我的团购提醒
                    FIGoodsDetailsViewController * vc = [[FIGoodsDetailsViewController alloc] init];
                    vc.str_goods_id = str_id;
                    vc.str_color_bg = @"#FBC900";//251 201 0
                    vc.m_b_is_notice=YES;
                    vc.m_b_is_have_toto=YES;
                    [self.navigationController pushViewController:vc animated:YES];
                }
                if (ba_type==2) {//我参加过的团购
                    /*
                    FIGoodsDetailsViewController * vc = [[FIGoodsDetailsViewController alloc] init];
                    vc.str_goods_id = str_id;
                    vc.str_color_bg = @"#FBC900";//251 201 0
                    [self.navigationController pushViewController:vc animated:YES];
                     */
                     BuyProductViewController * buy = [[BuyProductViewController alloc] init];
                     buy.stringurl = @"";
                     buy.stringid = str_id;
                     
                     buy.m_b_is_come_s=NO;
                     buy.m_b_is_have_say=NO;
                     NSDictionary * dic_groupon_status = [feed_data objectForKey:@"groupon_status"];
                     if (dic_groupon_status && [dic_groupon_status isKindOfClass:[NSDictionary class]])
                     {
                         buy.m_b_is_come_s=[[dic_groupon_status objectForKey:@"is_groupon"] boolValue];
                         buy.m_b_is_have_say = [[dic_groupon_status objectForKey:@"is_commented"] boolValue];
                         
                         buy.str_group_id=[dic_groupon_status objectForKey:@"id_groupon"];
                         buy.end_time = [[dic_groupon_status objectForKey:@"end_time"] doubleValue];
                     }
                    id bb_groupon = [feed_data objectForKey:@"groupon"];
                    if (bb_groupon && [bb_groupon isKindOfClass:[NSDictionary class]]) {
                        buy.str_money=[bb_groupon objectForKey:@"price"];
                    }
                    //
                    //share
                    buy.str_share_name = [feed_data objectForKey:@"title"];
                    SEFeedViewCell * cell = (SEFeedViewCell *)[tableView cellForRowAtIndexPath:indexPath];
                    if (cell) {
                        buy.image_share_ = cell.ggImage0.image;
                    }
                    
                     [self.navigationController pushViewController:buy animated:YES];
                }
                if (ba_type==4) {//我的心愿商品
                    BuyProductViewController * buy = [[BuyProductViewController alloc] init];
                    NSString * buy_url = [feed_data objectForKey:@"buy_url"];
                    if (buy_url == nil || [buy_url isKindOfClass:[NSString class]]) {
                        buy_url = @"http://www.baidu.com";
                    }
                    buy.stringurl = buy_url;
                    buy.stringid = str_id;
                    
                    buy.m_b_is_come_s=NO;
                    buy.m_b_is_have_say=NO;
                    NSDictionary * dic_groupon_status = [feed_data objectForKey:@"groupon_status"];
                    if (dic_groupon_status && [dic_groupon_status isKindOfClass:[NSDictionary class]]) {
                        
                        buy.m_b_is_come_s=[[dic_groupon_status objectForKey:@"is_groupon"] boolValue];
                        buy.m_b_is_have_say = [[dic_groupon_status objectForKey:@"is_commented"] boolValue];
                        
                        buy.str_group_id=[dic_groupon_status objectForKey:@"id_groupon"];
                        buy.end_time = [[dic_groupon_status objectForKey:@"end_time"] doubleValue];
                        
                        if ([dic_groupon_status objectForKey:@"price"] &&
                            [[dic_groupon_status objectForKey:@"price"] isKindOfClass:[NSString class]]) {
                            buy.str_money=[dic_groupon_status objectForKey:@"price"];
                        }
                    }
                    
                    //share
                    buy.str_share_name = [feed_data objectForKey:@"title"];
                    SEFeedViewCell * cell = (SEFeedViewCell *)[tableView cellForRowAtIndexPath:indexPath];
                    if (cell) {
                        buy.image_share_ = cell.ggImage0.image;
                    }
                    
                    [self.navigationController pushViewController:buy animated:YES];
                }
            }
        }
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end








@implementation SEFeedViewCell
{
    UIActivityIndicatorView * activity_view;
    
    UIView * temp_show_view;
    UIView * temp_load_view;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //基本显示
        if (temp_show_view==nil) {
            temp_show_view = [[UIView alloc] initWithFrame:self.bounds];
            //...时间日期
            if (self.dataLabel == nil) {
                self.dataLabel = [UILabel initWithCommonParameters:CGRectMake(5, 45, 60, 90)
                                                              text:@"49天前17:08"
                                                         textColor:[UIColor grayColor]
                                                          fontSize:12
                                                         isNewLine:YES];
                [temp_show_view addSubview:self.dataLabel];
            }
            //状态图片
            if (self.stateImage == nil) {
                UIView * timeLine = [[UIView alloc] initWithFrame:CGRectMake(52, 0, 2, 160)];
                timeLine.backgroundColor = RGBCOLOR(217, 206, 189);
                [temp_show_view addSubview:timeLine];
                
                self.btn_state = [[UIButton alloc] initWithFrame:CGRectMake(37, 8, 30, 30)];
                self.btn_state.backgroundColor=[UIColor clearColor];
                self.stateImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
                [self.btn_state addSubview:self.stateImage];
                
                [self.btn_state addTarget:self action:@selector(doActionGoPage:) forControlEvents:UIControlEventTouchUpInside ];
                
                [temp_show_view addSubview:self.btn_state];
            }
            //标题
            if (self.label_title == nil) {
                self.label_title = [UILabel initWithCommonParameters:CGRectMake(75, 3, 320-80, 30)
                                                                text:@""
                                                           textColor:[UIColor grayColor]
                                                            fontSize:14
                                                           isNewLine:NO];
                [temp_show_view addSubview:self.label_title];
            }
            //商品图片
            if (self.ggImage0==nil) {
                self.ggImage0 = [[UIImageView alloc] initWithFrame:CGRectMake(75+0*85, 35, 75, 75)];
                [temp_show_view addSubview:self.ggImage0];
                
                self.ggLabel0 = [UILabel initWithCommonParameters:CGRectMake(78+0*85, 105, 75, 30)
                                                             text:@"第一个商品"
                                                        textColor:[UIColor grayColor]
                                                         fontSize:11
                                                        isNewLine:NO];
                [temp_show_view addSubview:self.ggLabel0];
            }
            if (self.ggImage1==nil) {
                self.ggImage1 = [[UIImageView alloc] initWithFrame:CGRectMake(75+1*85, 35, 75, 75)];
                [temp_show_view addSubview:self.ggImage1];
                
                self.ggLabel1 = [UILabel initWithCommonParameters:CGRectMake(78+1*85, 105, 75, 30)
                                                             text:@"第一个商品"
                                                        textColor:[UIColor grayColor]
                                                         fontSize:11
                                                        isNewLine:NO];
                [temp_show_view addSubview:self.ggLabel1];
            }
            if (self.ggImage2==nil) {
                self.ggImage2 = [[UIImageView alloc] initWithFrame:CGRectMake(75+2*85, 35, 75, 75)];
                [temp_show_view addSubview:self.ggImage2];
                
                self.ggLabel2 = [UILabel initWithCommonParameters:CGRectMake(78+2*85, 105, 75, 30)
                                                             text:@"第一个商品"
                                                        textColor:[UIColor grayColor]
                                                         fontSize:11
                                                        isNewLine:NO];
                [temp_show_view addSubview:self.ggLabel2];
            }
            
            //...
            {
                if (self.ggLabel_SO_0 == nil) {
                    self.ggLabel_SO_0 = [UILabel initWithCommonParameters:CGRectMake(78, 125, 320-80, 30)
                                                                 text:@"第一个商品"
                                                            textColor:[UIColor grayColor]
                                                             fontSize:12
                                                            isNewLine:NO];
                    [temp_show_view addSubview:self.ggLabel_SO_0];
                }
                if (self.ggLabel_SO_1 == nil) {
                    self.ggLabel_SO_1 = [UILabel initWithCommonParameters:CGRectMake(78, 125, 320-80, 20)
                                                                     text:@"第一个商品"
                                                                textColor:RGBCOLOR(255, 198, 0)
                                                                 fontSize:12
                                                                isNewLine:NO];
                    [temp_show_view addSubview:self.ggLabel_SO_1];
                }
                if (self.ggLabel_SO_2 == nil) {
                    self.ggLabel_SO_2 = [UILabel initWithCommonParameters:CGRectMake(78, 140, 320-80, 20)
                                                                     text:@"第一个商品"
                                                                textColor:[UIColor grayColor]
                                                                 fontSize:12
                                                                isNewLine:NO];
                    [temp_show_view addSubview:self.ggLabel_SO_2];
                }
            }
            
            [self addSubview:temp_show_view];
        }
        //load显示
        if (temp_load_view == nil) {
            temp_load_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 80)];
            temp_load_view.backgroundColor=[UIColor clearColor];
            {
                UIView * timeLine = [[UIView alloc] initWithFrame:CGRectMake(52, 0, 2, 80)];
                timeLine.backgroundColor = RGBCOLOR(217, 206, 189);
                [temp_load_view addSubview:timeLine];
            }
            
            if (self.label_load==nil) {
                _label_load = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 40)];
                _label_load.backgroundColor=[UIColor clearColor];
                _label_load.textColor=[UIColor blackColor];
                _label_load.textAlignment=NSTextAlignmentCenter;
                _label_load.font=[UIFont systemFontOfSize:15];
                _label_load.text=@"加载更多";
                _label_load.userInteractionEnabled=NO;
                [temp_load_view addSubview:_label_load];
            }
            
            if (activity_view == nil) {
                activity_view = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(65, 0, 40, 40)];
                activity_view.activityIndicatorViewStyle=UIActivityIndicatorViewStyleGray;
            }
            [temp_load_view addSubview:activity_view];
            temp_load_view.hidden=YES;
            
            [self addSubview:temp_load_view];
        }
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

- (void)StateOP:(NSInteger)state
{
    self.type_state = state;
    
    temp_show_view.hidden=NO;
    temp_load_view.hidden=YES;
    
    self.ggLabel0.text=@"";
    self.ggLabel1.text=@"";
    self.ggLabel2.text=@"";
    self.ggImage0.image=nil;
    self.ggImage1.image=nil;
    self.ggImage2.image=nil;
    
    self.dataLabel.text=@"";
    self.label_title.attributedText=nil;
    self.label_title.text=@"";
    
    if (state==1) {
        self.stateImage.image=[UIImage imageNamed:@"feedState1"];
    } else if (state==2) {
        self.stateImage.image=[UIImage imageNamed:@"feedState0"];
    } else if (state==3) {
        self.stateImage.image=[UIImage imageNamed:@"feedState0"];
    } else if (state==4) {
        self.stateImage.image=[UIImage imageNamed:@"feedState3"];
    } else if (state==5) {
        self.stateImage.image=[UIImage imageNamed:@"feedState2"];
    } else {
        self.stateImage.image=[UIImage imageNamed:@"feedState3"];
    }
}

- (void)StateLoading:(NSInteger)state;
{
    temp_show_view.hidden=YES;
    temp_load_view.hidden=NO;
    
    if (state==0) {
        [activity_view stopAnimating];
        self.label_load.text=@"没有信息了";
    } else {
        [activity_view startAnimating];
        self.label_load.text=@"加载更多";
    }
}

- (void)doActionGoPage:(id)sender
{
    if (self.delegate_go_page) {
        [self.delegate_go_page goRefPage:self.type_state];
    }
}

@end














